package com.wang.wemedia.service.impl;

import com.wang.common.model.enums.TaskTypeEnum;
import com.wang.schedule.api.client.ScheduleClient;
import com.wang.schedule.api.model.Task;
import com.wang.util.ProtostuffUtil;
import com.wang.wemedia.api.model.pojo.WmNews;
import com.wang.wemedia.service.WmNewsTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 发布文章集成延迟队列接口
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WmNewsTaskServiceImpl  implements WmNewsTaskService {

    @Autowired
    private ScheduleClient scheduleClient;

    /**
     * 添加任务到延迟队列中
     * @param id          文章的id
     * @param publishTime 发布的时间  可以做为任务的执行时间
     */
    @Override
    @Async
    public void addNewsToTask(Integer id, Date publishTime) {

        log.info("开始添加任务到延迟服务中");

        Task task = new Task();
        task.setExecuteTime(publishTime.getTime());
        task.setTaskType(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType());
        task.setPriority(TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
        WmNews wmNews = new WmNews();
        wmNews.setId(id);
        task.setParameters(ProtostuffUtil.serialize(wmNews));

        scheduleClient.addTask(task);

        log.info("添加任务结束");

    }
    
}